package com.hd.crdao.adapter;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.hd.crdao.R;
import com.hd.crdao.base.MyBaseAdapter;
import com.hd.crdao.bean.CarInfoListBean;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.ALog;
import com.hd.crdao.utils.UIUtils;
import com.hd.crdao.wrapper.GlideImageUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 *
 *@author Programming
 *@date 2017/5/26 0026
 */
public class MyCarSourceAdapter extends MyBaseAdapter<CarInfoListBean> {


    public ArrayList<Boolean> list;
    public String[] ids;
    private String state;
    private HashMap<Integer, Boolean> mHashMap = new HashMap<>();

    public MyCarSourceAdapter(List<CarInfoListBean> dataSets, String state) {
        // 初始化数据
        super(dataSets);
        this.state = state;
        ids = new String[dataSets.size()];
        for (int i = 0; i < dataSets.size(); i++) {     //初始化为FALSE
            mHashMap.put(i,false);          //对所有的checkbox进行初始化
        }

    }

    public void selectAll(boolean isDeselectAll) {
        for (int i = 0; i < mDataSets.size(); i++) {     //初始化为FALSE
            mHashMap.put(i,isDeselectAll);          //对所有的checkbox进行初始化
        }
    }



    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(UIUtils.getContext(), R.layout.mycarsource_item, null);
            holder = new ViewHolder();
            holder.mCarDesc = (TextView) convertView.findViewById(R.id.car_desc);
            holder.mSelectoperation = (CheckBox) convertView.findViewById(R.id.selectoperation);
            holder.mCarImage = (ImageView) convertView.findViewById(R.id.car_image);
            holder.mCarPrice = (TextView) convertView.findViewById(R.id.car_price);
            holder.mCarTime = (TextView) convertView.findViewById(R.id.car_time);
            holder.mCarLocation = (TextView) convertView.findViewById(R.id.car_location);
            holder.mCarLong = (TextView) convertView.findViewById(R.id.car_long);
            holder.mState = (TextView) convertView.findViewById(R.id.state);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final CarInfoListBean carInfoListBean = mDataSets.get(position);
        //标题
        if (carInfoListBean.getVehConfig() == null) {
            holder.mCarDesc.setText("未选择车辆的型号");
        } else {
            holder.mCarDesc.setText(carInfoListBean.getVehConfig().getVcTypenme());
        }
        if (TextUtils.isEmpty(carInfoListBean.getCoverPath())) {
            holder.mCarImage.setImageResource(R.drawable.logo_moren_2x);
        } else {
            GlideImageUtils.loadImage(Constants.IMAGEURL + carInfoListBean.getCoverPath(),R.drawable.logo_moren_2x,holder.mCarImage);
        }
        holder.mCarLong.setText(String.format("%s万公里", carInfoListBean.getCiMileage()));
        holder.mCarLocation.setText(carInfoListBean.getCiPlatearea());
        String ciFailtime = carInfoListBean.getCiFailtime();
        ALog.d(ciFailtime);
        if (ciFailtime != null) {
            if (ciFailtime.length() == 10) {
                int lastIndexOf = ciFailtime.lastIndexOf("-");
                holder.mCarTime.setText(ciFailtime.substring(0, lastIndexOf));
            } else {
                holder.mCarTime.setText(ciFailtime);
            }
            holder.mCarTime.setVisibility(View.VISIBLE);
        } else {
            holder.mCarTime.setVisibility(View.GONE);
            holder.mCarTime.setText("未选择");
        }
        holder.mCarPrice.setText(String.format("%s万", carInfoListBean.getCiExpectprice()));
        if (Integer.parseInt(state) == 3) {
            holder.mState.setVisibility(View.VISIBLE);
        } else {
            holder.mState.setVisibility(View.GONE);
        }
        holder.mSelectoperation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckBox checkbox = (CheckBox) v;
                notifyDataSetChanged();
                if (checkbox.isChecked()) {
                    mHashMap.put(position,true);
                    ids[position] = carInfoListBean.getId();
                }else{
                    mHashMap.put(position,false);
                }
            }
        });
        Boolean isChecked = mHashMap.get(position);
        if (isChecked) {
            ids[position] = carInfoListBean.getId();
        }
        holder.mSelectoperation.setChecked(isChecked);

        return convertView;
    }



    public class ViewHolder {

         CheckBox mSelectoperation;
        ImageView mCarImage;
        TextView mCarPrice;
        TextView mCarDesc;
        TextView mCarTime;
        TextView mCarLocation;
        TextView mCarLong;
        TextView mState;
    }


}
